﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.ObjectModel;
using Catfood.Shapefile;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.Projections;
using GMap.NET.WindowsPresentation;
using System.Globalization;

namespace RZD_maps.Modules.Stations.Model
{
    public static class StationUtil
    {
        public static ObservableCollection<StationCategory> CreateShop()
        {
            ObservableCollection<StationCategory> list = new ObservableCollection<StationCategory>();
            StationCategory books = new StationCategory("Станция 1");
            StationCategory movies = new StationCategory("Станция 2");
            StationCategory music = new StationCategory("Станция 3");

            //root categories
            list.Add(books);
            list.Add(movies);
            list.Add(music);

            //2nd level items on all categories
            books.SubCategories.Add(new StationCategory("Элемент1 ст1", books));
            books.SubCategories.Add(new StationCategory("Элемент2 ст1", books));
            books.SubCategories.Add(new StationCategory("Элемент3 ст1", books));

            movies.SubCategories.Add(new StationCategory("Элемент1 ст2", movies));
            movies.SubCategories.Add(new StationCategory("Элемент2 ст2", movies));
            movies.SubCategories.Add(new StationCategory("Элемент3 ст2", movies));
            movies.SubCategories.Add(new StationCategory("Элемент4 ст2", movies));

            music.SubCategories.Add(new StationCategory("Элемент1 ст3", music));
            music.SubCategories.Add(new StationCategory("Элемент2 ст3", music));
            music.SubCategories.Add(new StationCategory("Элемент3 ст3", music));
            music.SubCategories.Add(new StationCategory("Элемент4 ст3", music));

            return list;
        }

        public static string DecodeFromUtf8(string utf8String)
        {
            // Get UTF8 bytes by reading each byte with ANSI encoding
            byte[] utf8Bytes = Encoding.Default.GetBytes(utf8String);

            // Convert UTF8 bytes to UTF16 bytes
            byte[] utf16Bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Bytes);

            // Return UTF16 bytes as UTF16 string
            string good = Encoding.Unicode.GetString(utf16Bytes);

            return good;

        }


        public static ObservableCollection<StationCategory> CreateStations()
        {
            ObservableCollection<StationCategory> list = new ObservableCollection<StationCategory>();
            Shapefile myshapefile = new Shapefile();
            ShapePoint mypoly;
            double ESRIShapepolylat;
            double ESRIShapepolylong;

            string ESRIMunName;



            myshapefile.Open(@"railway-station-point.shp");
            List<PointLatLng> routePoints = new List<PointLatLng>();

            string area = "Орехово-Зуево";
            GeoCoderStatusCode status = GeoCoderStatusCode.Unknow;
            PointLatLng? pos = new PointLatLng(55.8062181, 38.9791242);// new PointLatLng(54.415238, 40.359397);// new PointLatLng(55.8062181, 38.9791242); //GMapProviders.GoogleMap.GetPoint("Россия, Московская область, " + area, out status);
            double radius_area = 8.8;


            foreach (var Shapes in myshapefile)
            {
                ESRIMunName = Shapes.GetMetadata("name").Trim();
                ESRIMunName = DecodeFromUtf8(ESRIMunName);

                mypoly = myshapefile.Current as ShapePoint;
                if (mypoly == null) continue;
                NumberFormatInfo provider = new NumberFormatInfo();
                provider.NumberDecimalSeparator = ".";
                provider.NumberGroupSeparator = ",";
                provider.NumberGroupSizes = new int[] { 3 };

                ESRIShapepolylat = mypoly.Point.Y;
                ESRIShapepolylong = mypoly.Point.X;

                PointLatLng pt = new PointLatLng(ESRIShapepolylat, ESRIShapepolylong);

                //if (GMapProviders.OpenStreetMap.Projection.GetDistance(pos.Value, pt) > radius_area) continue;
                if (MercatorProjection.Instance.GetDistance(pos.Value, pt) > radius_area) continue;
                
                StationCategory station = new StationCategory(ESRIMunName);
                station.Lat = ESRIShapepolylat;
                station.Lng = ESRIShapepolylong;
                list.Add(station);
                string data;
                data = string.Format("Long: {0}", ESRIShapepolylong);
                station.SubCategories.Add(new StationCategory(data, station));
                data = string.Format("Lat: {0}", ESRIShapepolylat);
                station.SubCategories.Add(new StationCategory(data, station));
            }
            return list;
        }
    }
}
